require "tiledmap"
require "libraries.third-party.class" --摇杆需要的

gKeyPressed = {}
gCamX,gCamY = 100,100

--loveframe需要增加的
demo = {}
-- table to store available examples
demo.examples = {}
demo.examples[1] = {registered = {}}
demo.centerarea = {5, 40, 540, 555}

--汽车运动，需要预先定义一系列坐标
var = 10   -- arbitrary starting value
rate = 10   -- change to change the rate at which the var is changed，汽车的速度
local Car1X=404
local Car1Y=404  

function loadMap(path)
  TiledMap_Load(path)
  gCamX,gCamY = gMapWidth * gTileWidth / 2, gMapHeight * gTileHeight / 2
end

function love.load()
    font = love.graphics.newFont("/resources/fonts/SourceHanSansCN-Normal.otf",24)  --加载字体
	loadMap("map/map01.tmx")
  
    --绘制摇杆
  bg = love.graphics.newImage("resources/images/layout.png")
	center = love.graphics.newImage("resources/images/center.png")
	hero = love.graphics.newImage("resources/images/aircraft-ne.png")	
	gr=love.graphics
	point = {x=200,y=200}	
	joy = require "libraries/third-party/_joystick"(bg,center)
	joy:setPoint(130,love.graphics.getHeight()-130)	
	
	--loveframes
	loveframes = require("libraries.loveframes")
	require("examples.frame")
	--直接弹出公寓
    for k, v in ipairs(demo.examples) do
		for key, value in ipairs(v.registered) do
				value.func()--直接弹出公寓
		end
	end
	--loveframe-end
	
	
end



function love.keypressed( key, unicode ) 
	gKeyPressed[key] = true 
	if (key == "escape") then os.exit(0) end
	if (key == " ") then -- space = next mal
		gMapNum = (gMapNum or 1) + 1
		if (gMapNum > 3) then gMapNum = 1 end
		loadMap(string.format("map/map%02d.tmx",gMapNum))
	end
end

function love.update( dt )
	local s = 500*dt
	if (gKeyPressed.up) then gCamY = gCamY - s end
	if (gKeyPressed.down) then gCamY = gCamY + s end
	if (gKeyPressed.left) then gCamX = gCamX - s end
	if (gKeyPressed.right) then gCamX = gCamX + s end
  -- Keep camera within map bounds
  gCamX = math.min(math.max(gCamX, 0), gMapWidth * gTileWidth)
  gCamY = math.min(math.max(gCamY, 0), gMapHeight * gTileHeight)
  
     --摇杆更新
  if joy.dir == 1 then 		
	elseif joy.dir == 2 then 
		point.y = point.y + 300*dt * joy.power
		gCamY = gCamY + 300*dt * joy.power
	elseif joy.dir == 3 then 
	elseif joy.dir == 4 then 
		point.x = point.x - 300*dt
		gCamX = gCamX - 300*dt
	elseif joy.dir == 6 then 
		point.x = point.x + 300*dt
		gCamX = gCamX + 300*dt
	elseif joy.dir == 7 then 
	elseif joy.dir == 8 then 
		point.y = point.y - 300*dt
		gCamY = gCamY - 300*dt
	elseif joy.dir == 9 then 
	end
	--loveframes
	loveframes.update(dt)
	
	--汽车运动，需要预先定义一系列坐标
	  --var = var - (dt * rate)
      --更新汽车坐标与汽车摆放方向
      Car1Y=Car1Y-(dt * rate) 
end

function love.draw()
  love.graphics.print('arrow-keys=scroll, space=next map', 50, 50)
  --此处颜色我做了0~1的修正
	love.graphics.setBackgroundColor(80/255,80/255,80/255)
	TiledMap_DrawNearCam(gCamX,gCamY)
  
  joy:draw()--摇杆
  

  
   --绘制汽车行驶
        mapleft=Car1X
        maptop=Car1Y
        --绘制贴图
          love.graphics.setBackgroundColor(1,1,1)
          image2 = love.graphics.newImage( "/resources/images/Car-1.png" )
          love.graphics.draw(image2,mapleft,maptop)
  --绘制火车行驶
  
  --绘制飞机飞行路线
  
      --获得屏幕的宽与高
  W,H = love.window.getDesktopDimensions()
 
  --绘制监狱
       love.graphics.setColor(1, 1, 1)
       love.graphics.setFont(font)
       love.graphics.print("监狱",W/2-352/2,H/2-262/2-70)
  
  --最后绘制报表层
  loveframes.draw()
end

--摇杆
if love.system.getOS() == "Windows" then


	function love.mousepressed(x,y)
		--joy:touchpressed(1,x,y)
	end



	function love.mousereleased(x,y)
		--joy:touchreleased(1,x,y)
	end

elseif love.system.getOS() == "Android" then 

	function love.touchpressed(...)
		joy:touchpressed(...)
	end

	function love.touchmoved(...)
		joy:touchmoved(...)
	end

	function love.touchreleased(...)
	joy:touchreleased(...)
	end
end

--loveframes
	function love.mousemoved(x,y)
		joy:touchmoved(1,x,y)
	end

function love.mousepressed(x, y, button)
	joy:touchpressed(1,x,y)
	loveframes.mousepressed(x, y, button)
end

function love.mousereleased(x, y, button)
	joy:touchreleased(1,x,y)
	loveframes.mousereleased(x, y, button)
end



function love.keyreleased(key)
	loveframes.keyreleased(key)
	--用键盘控制汽车行驶
   if key == "escape" then
      love.event.quit()
   end
      if love.keyboard.isDown("down") then   -- reduce the value
      Car1Y = Car1Y -  rate
   end
   if love.keyboard.isDown("up") then   -- increase the value
      Car1Y = Car1Y + rate
   end
	
end

function love.textinput(text)
	loveframes.textinput(text)
end

--注册
function demo.RegisterExample(example)	
	local examples = demo.examples
	for k, v in ipairs(examples) do
			table.insert(examples[k].registered, example)
	end
end